package com.example.framework.picasso.activity;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapShader;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.framework.R;
import com.squareup.picasso.Picasso;
import com.squareup.picasso.Transformation;


public class PicassoActivity extends Activity
{
      
      TextView tvTitle;
      Button btPicassoBase;
      Button btPicassoListview;
      Button btPicassoTranformations;
      ImageView ivPicassoResult1;
      ImageView ivPicassoResult2;
      ImageView ivPicassoResult3;
      
      @Override
      protected void onCreate(Bundle savedInstanceState)
      {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_picasso);
            
            tvTitle = findViewById(R.id.tv_title);
            btPicassoBase = findViewById(R.id.bt_picasso_base);
            btPicassoListview = findViewById(R.id.bt_picasso_listview);
            btPicassoTranformations = findViewById(R.id.bt_picasso_tranformations);
            ivPicassoResult1 = findViewById(R.id.iv_picasso_result1);
            ivPicassoResult2 = findViewById(R.id.iv_picasso_result2);
            ivPicassoResult3 = findViewById(R.id.iv_picasso_result3);
            
            
            tvTitle.setText("Picasso");
            
            btPicassoBase.setOnClickListener(new View.OnClickListener()
            {
                  @Override
                  public void onClick(View v)
                  {
                        // 基本用法 普通加载图片  需要 https
                        Picasso.get().load("https://n.sinaimg.cn/translate/20160819/9BpA-fxvcsrn8627957.jpg").transform(new Transformation()
                        {
                              @Override
                              public Bitmap transform(Bitmap source)
                              {
                                    int size = Math.min(source.getWidth(), source.getHeight());
                                    int x = (source.getWidth() - size) / 2;
                                    int y = (source.getHeight() - size) / 2;
                                    Bitmap squaredBitmap = Bitmap.createBitmap(source, x, y, size, size);
                                    if (squaredBitmap != source) {
                                          source.recycle();
                                    }
                                    Bitmap bitmap = Bitmap.createBitmap(size, size, source.getConfig());
                                    Canvas canvas = new Canvas(bitmap);
                                    Paint paint = new Paint();
                                    BitmapShader shader = new BitmapShader(squaredBitmap,
                                            BitmapShader.TileMode.CLAMP, BitmapShader.TileMode.CLAMP);
                                    paint.setShader(shader);
                                    paint.setAntiAlias(true);
                                    float r = size / 2f;
                                    canvas.drawCircle(r, r, r, paint);
                                    squaredBitmap.recycle();
                                    return bitmap;
                              }
                              
                              @Override
                              public String key() {
                                    return "circle";
                              }
                        }).into(ivPicassoResult1);
                        
                        //缩放的方式加载图片
                        Picasso.get().load("https://n.sinaimg.cn/translate/20160819/9BpA-fxvcsrn8627957.jpg").resize(100, 100).into(ivPicassoResult2);
                        
                        //选择180度
                        Picasso.get().load("https://n.sinaimg.cn/translate/20160819/9BpA-fxvcsrn8627957.jpg").rotate(180).into(ivPicassoResult3);
                  }
            });
            
            btPicassoListview.setOnClickListener(new View.OnClickListener()
            {
                  @Override
                  public void onClick(View v)
                  {
                        Intent intent = new Intent(PicassoActivity.this, PicassoListviewActivity.class);
                        startActivity(intent);
                  }
            });

            btPicassoTranformations.setOnClickListener(new View.OnClickListener()
            {
                  @Override
                  public void onClick(View v)
                  {
                        Intent intent = new Intent(PicassoActivity.this, PicassoTransfromationsActivity.class);
                        startActivity(intent);
                  }
            });
      }
}
